
#include "regex_prefix_matcher.hpp"
#include "lexer.hpp"
#include <iostream>
#include <regex.h>
#include <sys/types.h>

using namespace std;

size_t Matcher::last_match_length = 0;

bool Matcher::prefixMatch(const string &pattern, const string &text) 
{   
    int reti;
    regex_t regex;
    regmatch_t match;
    
    regcomp(&regex, pattern.c_str(), 0);

    reti = regexec(&regex, text.c_str(), 1, &match, 0);

    if (reti == REG_NOMATCH or match.rm_so) {
        last_match_length = 0;
        return false;
    }

    last_match_length = match.rm_eo - match.rm_so;
    return true;
}

size_t Matcher::getLastMatchLength()
{
    return last_match_length;
}
